<?php
session_start();

function __autoload($name){
	require $name . '.class.php';
}

define("SCRIPTBASE", realpath(dirname(__FILE__) . '/..') . "/");

require SCRIPTBASE . '_private/config.php';
if(!defined('CONFIG_FILE'))
	define('CONFIG_FILE', SCRIPTBASE.'_private/config.php');
set_include_path(SCRIPTBASE.'php_classes'.PATH_SEPARATOR.get_include_path());

$config = Config::getInstance();


function Core_isAdmin(){
	if(!isset($_SESSION['userdata'])) return false;
	if(Core_hasCredentials('.core', '.admin') || Core_hasCredentials('.core', '.superadmin')) return true;
	//if(!isset($_REQUEST['login_msg']))	
	//	$_REQUEST['login_msg'] = 'permissiondenied';
	return false;
}

function Core_hasCredentials($plugin, $permission){
	if(!isset( $_SESSION['userdata']['permissions'][$plugin][$permission] )) return false;
	else return true;	
}

function hasPermission($plugin, $permission){
	if(!Core_hasCredentials($plugin,$permission) && !Core_hasCredentials('.core','.superadmin')){
		header(' ', true, 401);
		die('U heeft onvoldoende rechten voor deze actie');
	}
}

//theme vars
if (isset($config->siteOptions['theme-dir']) && $config->siteOptions['theme-dir']) {
	define('THEME_DIR', $config->siteOptions['theme-dir']);
}
else {
	define('THEME_DIR', SCRIPTBASE.'skins');
}
if (isset($config->siteOptions['theme']) && $config->siteOptions['theme']) {
	define('THEME', $config->siteOptions['theme']);
}


function Core_smartySetup($compile_dir) {
	require_once SCRIPTBASE . '/inc/Smarty-3.1.12/libs/Smarty.class.php';
	
	$smarty = new Smarty;
	
	$smarty->setCaching(false);
	//$smarty->registerFilter('variable','htmlspecialchars');
	/*
	$smarty->assign('GLOBALS', $GLOBALS);
	$smarty->assign('LANGUAGE', @$_SESSION['language']);
	$smarty->assign('LOCATIONNAME', @$_SESSION['location']['name']);
	$smarty->registerPlugin('function', 'BREADCRUMBS', 'Template_breadcrumbs');
	$smarty->registerPlugin('function', 'LANGUAGES', 'Core_languagesGetUi');
	$smarty->registerPlugin('function', 'LOCATIONSELECTOR', 'Core_locationsGetUi');
	$smarty->registerPlugin('function', 'LOGO', 'Template_logoDisplay');
	$smarty->registerPlugin('function', 'MENU', 'menuDisplay');
	$smarty->assign('QRCODE', '/a/f=qrCode/id='.$PAGEDATA->id);
	$smarty->registerPlugin('function', 'nuMENU', 'Core_menuShowFg');
	foreach ($PLUGINS as $pname=>$plugin) {
		if (isset($plugin['frontend']['template_functions'])) {
			foreach ($plugin['frontend']['template_functions'] as $fname=>$vals) {
				$smarty->registerPlugin('function', $fname, $vals['function']);
			}
		}
	}
	 */
	$smarty->compile_dir=SCRIPTBASE.'cache/'.$compile_dir;
	$smarty->registerPlugin('function', 'menu', 'Core_menuDisplay');
	return $smarty;
	
}

/**
  * clear a cache or all caches
  *
  * @param string $type the cache to clear
  *
  * @return null
  */
function Core_cacheClear($type='') {
	if (!is_dir(SCRIPTBASE.'/cache/'.$type)) {
		return;
	}
	$d=new DirectoryIterator(SCRIPTBASE.'/cache/'.$type);
	foreach ($d as $f) {
		$f=$f->getFilename();
		if ($f=='.' || $f=='..') {
			continue;
		}
		if (is_dir(SCRIPTBASE.'/cache/'.$type.'/'.$f)) {
			Core_cacheClear($type.'/'.$f);
			rmdir(SCRIPTBASE.'/cache/'.$type.'/'.$f);
		}
		else {
			unlink(SCRIPTBASE.'/cache/'.$type.'/'.$f);
		}
	}
}

/**
  * retrieve a cached variable if it exists
  *
  * @param string $type type of cache
	* @param string $md5  unique identifier of the cache
  *
  * @return mixed the variable if it was cached, or false
  */
function Core_cacheLoad($type, $md5) {
	if (strlen($md5)>32) {
		$md5=md5($md5);
	}
	if (file_exists(SCRIPTBASE.'/cache/'.$type.'/'.$md5)) {
		return json_decode(
			file_get_contents(SCRIPTBASE.'/cache/'.$type.'/'.$md5),
			true
		);
	}
	return false;
}

/**
  * cache a variable
  *
  * @param string $type type of cache (page, product, etc)
	* @param string $md5  unique identifier - not necessarily an MD5
	* @param mixed  $vals the variable to encode and cache
  *
  * @return null
  */
function Core_cacheSave($type, $md5, $vals) {
	if (strlen($md5)>32) {
		$md5=md5($md5);
	}
	if (!is_dir(SCRIPTBASE.'/cache/'.$type)) {
		mkdir(SCRIPTBASE.'/cache/'.$type, 0777, true);
	}
	file_put_contents(
		SCRIPTBASE.'/cache/'.$type.'/'.$md5,
		json_encode($vals)
	);
}

//helpfunction to print an arie
function pa($a){
	echo '<pre>';
	print_r($a);
	echo '</pre>';
}

